﻿using mojoPortal.Business;
using mojoPortal.Web;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Web.UI;

namespace blackhouse.mojoPortalExtend {
    public class SelectLanguage : UserControl {

        #region Fields

        private CultureInfo[] lanuages;

        #endregion

        #region Properties

        public string CssClass { get; set; }

        #endregion

        #region Constructors

        public SelectLanguage() {
            this.CssClass = "select-language";
        }

        #endregion

        #region Methods

        private CultureInfo[] GetLanguagesBasedOnWebConfig() {
            object objSiteLanguages = System.Web.HttpRuntime.Cache.Get("siteLanguages");
            if (objSiteLanguages != null) {
                CultureInfo[] siteLanguages = (CultureInfo[])objSiteLanguages;
                if (siteLanguages != null) return siteLanguages;
            }
            List<CultureInfo> cultures = new List<CultureInfo>();
            if (WebConfigSettings.UseCultureOverride) {
                int siteId = 1;
                while (true) {
                    string key = "site" + siteId + "culture";
                    if (!ConfigurationManager.AppSettings.AllKeys.Contains(key))
                        break;
                    string siteCulture = ConfigurationManager.AppSettings["site" + siteId + "culture"];
                    if (string.IsNullOrEmpty(siteCulture))
                        continue;
                    try {
                        CultureInfo culture = CultureInfo.GetCultureInfo(siteCulture);
                        cultures.Add(culture);
                    }
                    catch (CultureNotFoundException) {
                        continue;
                    }
                }

            }
            CultureInfo[] aCultures = cultures.ToArray();
            System.Web.HttpRuntime.Cache.Add("siteLanguages", aCultures, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), System.Web.Caching.CacheItemPriority.AboveNormal, null);
            return aCultures;
        }

        #endregion
        #region UserControl Members

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);


            this.lanuages = this.GetLanguagesBasedOnWebConfig();
        }

        protected override void Render(HtmlTextWriter writer) {
            if (lanuages.Length < 2) return; // Nie pokazujemy kontrolki, jak nie ma conajmniej dwóch flag
            if (!string.IsNullOrEmpty(this.CssClass))
                writer.AddAttribute(HtmlTextWriterAttribute.Class, this.CssClass);
            writer.RenderBeginTag(HtmlTextWriterTag.Ul);
            foreach (CultureInfo culture in this.lanuages) {
                this.RenderLanguageItem(writer, culture);
            }
            writer.RenderEndTag();
        }

        private void RenderLanguageItem(HtmlTextWriter writer, CultureInfo language) {
            writer.RenderBeginTag(HtmlTextWriterTag.Li);

            writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            writer.RenderEndTag();
            writer.RenderEndTag();
            //SiteSettings siteSetting = new SiteSettings(2);
            //SiteUtils.GetNavigationSiteRoot(siteSetting);   DO UŻYCIA JAK BĘDĄ SITY.
        }

        #endregion

    }
}
